package com.cloudeasy.post.controller;

import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.api.vo.post.PostVO;
import com.cloudeasy.api.vo.post.SimpleUserInfoVO;
import com.cloudeasy.core.bean.JwtParser;
import com.cloudeasy.post.po.Post;
import com.cloudeasy.post.po.PostCollect;
import com.cloudeasy.post.service.PostCollectService;
import com.cloudeasy.post.service.PostService;
import com.cloudeasy.post.service.expand.SimpleUserInfoService;
import com.cloudeasy.post.util.PostUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/collect")
public class PostCollectController {
    private final PostService postService;

    private final PostCollectService postCollectService;

    private final SimpleUserInfoService simpleUserInfoService;

    private final JwtParser jwtParser;

    @Autowired
    public PostCollectController(PostService postService, PostCollectService postCollectService, SimpleUserInfoService simpleUserInfoService, JwtParser jwtParser) {
        this.postService = postService;
        this.postCollectService = postCollectService;
        this.simpleUserInfoService = simpleUserInfoService;
        this.jwtParser = jwtParser;
    }

    /**
     * 收藏帖子
     *
     * @param authentication {@link Authentication}
     * @param postId         帖子Id
     * @return {@link Result<Boolean>}
     */
    @PostMapping("/{postId}")
    public Result<Boolean> collect(Authentication authentication,
                                   @PathVariable("postId") String postId) {
        String userId = (String) jwtParser.getOnBody(authentication, "userId");
        return ResultUtil.success(postCollectService.collect(userId, postId));
    }

    /**
     * 取消收藏帖子
     *
     * @param authentication {@link Authentication}
     * @param postId         帖子Id
     * @return {@link Result<Boolean>}
     */
    @DeleteMapping("/{postId}")
    public Result<Boolean> removeCollect(Authentication authentication,
                                         @PathVariable("postId") String postId) {
        String userId = (String) jwtParser.getOnBody(authentication, "userId");
        return ResultUtil.success(postCollectService.removeCollect(userId, postId));
    }

    /**
     * 查看自己收藏的帖子
     *
     * @param authentication {@link Authentication}
     * @return {@link List<PostVO>}的{@link Result}
     */
    @GetMapping
    public Result<List<PostVO>> ownCollectPost(Authentication authentication) {
        String userId = (String) jwtParser.getOnBody(authentication, "userId");
        List<String> postIds = postCollectService.listByUserId(userId).stream().map(PostCollect::getPostId).distinct().collect(Collectors.toList());
        if (postIds.size() == 0) {
            return ResultUtil.success();
        }
        List<Post> posts = new ArrayList<>(postService.listByIds(postIds));
        List<String> authorIds = posts.stream().map(Post::getAuthorId).distinct().collect(Collectors.toList());
        List<SimpleUserInfoVO> simpleUserInfoVOS = simpleUserInfoService.obtainSimpleUserInfo(authorIds);

        return ResultUtil.success(PostUtil.convert(posts, simpleUserInfoVOS));
    }
}
